import Vue from 'vue'
import Vuex from 'vuex'
import ajax, { tacticURL } from '@/plugins/uni-ajax'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
	userinfo:{},//用户信息
	// storage:uni.getStorageSync('Login'),
	article:'',//文章id
    version:{},//版本信息
	terraceId:{terraceId:'1'},//平台id
	sybom:{sybom:0}
  },
  getters: {
   
  },
  mutations: {
	  setState(state, payload) {
	    if (payload.state) state[payload.state] = payload.value
	  },
  // 更改用户资产
  setUser(state, payload) {
    const value = payload ? { ...state.user, ...payload } : {}
    state.userinfo = value
  },
  },
  actions: {
	  getUser({ commit, state }) {
	    return new Promise(resolve => {
	      ajax.post('user/getUserInfo')
	        .then(res => {
	          if (res.data.code === 1) {
	            const userInfo = res.data.data
				
	            commit('setUser', userInfo)
	            resolve(userInfo)
	          } else resolve(null)
	        })
	        .catch(err => resolve(null))
	    })
	  },
  }
})
Vue.prototype.$state = new Proxy(
  {},
  {
    get(target, state) {
      return store.state[state]
    },
    set(target, state, value) {
      store.commit('setState', { state, value })
      return true
    }
  }
)

export default store
